//
//  HXAddressManager.m
//  HXProvincialCitiesCountiesPickerview
//  github:https://github.com/huangxuan518 博客：blog.libuqing.com
//  Created by 黄轩 on 16/7/8.
//  Copyright © 2016年 黄轩. All rights reserved.
//

#import "HXAddressManager.h"
#import "DbDao.h"
#import "CityArea.h"
@interface HXAddressManager ()

@end

@implementation HXAddressManager

+ (instancetype)shareInstance {
    static HXAddressManager *_addressManager = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _addressManager = [[self alloc] init];
    });
    return _addressManager;
}

- (NSArray *)provinceDicAry {
    if (!_provinceDicAry) {
        _provinceDicAry = [NSMutableArray array];
        NSMutableArray * addressArray = [NSMutableArray array];
        DbDao *dao = [DbDao sharedInstance];
        NSArray *provinceArr = [dao getCityArea:@"select * from sys_area where a_level = 3"];
        for (CityArea * proArea in provinceArr) {
             NSArray *cityArr = [dao getCityArea:[NSString stringWithFormat:@"select * from sys_area where a_parent_id = %ld",proArea.a_id]];
            NSMutableDictionary * provinceDic = [NSMutableDictionary dictionary];
            NSMutableArray * cityArray = [NSMutableArray array];
            for (CityArea *cityArea in cityArr) {
                NSArray *sectionArr = [dao getCityArea:[NSString stringWithFormat:@"select * from sys_area where a_parent_id = %ld",cityArea.a_id]];
                NSMutableDictionary * cityDic = [NSMutableDictionary dictionary];
                [cityDic setObject:cityArea forKey:@"city"];
                [cityDic setObject:sectionArr forKey:@"areas"];
                [cityArray addObject:cityDic];

                if (cityArea==cityArr.lastObject) {
                    [provinceDic setObject:proArea forKey:@"state"];
                    [provinceDic setObject:cityArray forKey:@"cities"];
                    [addressArray addObject:provinceDic];
                }
            }
      }
        _provinceDicAry = addressArray;
    }
    return _provinceDicAry;
}

@end
